package com.spzx.order.controller;

import com.github.pagehelper.PageHelper;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.order.domain.OrderForm;
import com.spzx.order.api.domain.OrderInfo;
import com.spzx.order.service.OrderInfoService;
import com.spzx.order.vo.TradeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author spzx
 * @since 2024-12-26
 */
@Tag(name = "订单结算")
@RestController
@RequestMapping("/orderInfo")
public class OrderInfoController extends BaseController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 订单结算 提交订单
     * @return
     */
    @Operation(summary = "订单结算")
    @GetMapping("/trade")
    public AjaxResult orderTradeData() {
        TradeVo tradeVo = orderInfoService.orderTradeData();
        return AjaxResult.success(tradeVo);
    }

    /**
     * 下单
     * @param orderForm
     * @return
     */
    @Operation(summary = "订单下单")
    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody OrderForm orderForm) {
        Long submitOrder = orderInfoService.submitOrder(orderForm);
        return AjaxResult.success(submitOrder);
    }

    @Operation(summary = "立即购买")
    @GetMapping("/buy/{skuId}")
    public AjaxResult buy(@PathVariable("skuId") Long skuId) {
        TradeVo tradeVo = orderInfoService.buy(skuId);
        return AjaxResult.success(tradeVo);
    }

    @Operation(summary = "获取用户订单分页列表")
    @GetMapping("/userOrderInfoList/{pageNum}/{pageSize}")
    public TableDataInfo getUserOrderInfoList(
            //required : true 代表前端必须传递此参数，否则报错 默认为false
            @Parameter(name = "pageNum", description = "当前页码", required = true)
            @PathVariable("pageNum") Integer pageNum,
            @Parameter(name = "pageSize", description = "每页条数", required = true)
            @PathVariable("pageSize") Integer pageSize,
            @Parameter(name = "orderStatus", description = "订单状态", required = false)
            @RequestParam(required = false, defaultValue = "") Integer orderStatus) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderInfo> orderInfoList = orderInfoService.getUserOrderInfoList(orderStatus);
        return getDataTable(orderInfoList);
    }

    @Operation(summary = "获取订单详情")
    @GetMapping("/getOrderInfo/{orderId}")
    public AjaxResult getOrderInfo(@PathVariable("orderId") Long orderId) {
        OrderInfo orderInfo = orderInfoService.getById(orderId);
        return AjaxResult.success(orderInfo);
    }

    @Operation(summary = "取消订单")
    @GetMapping("/cancelOrder/{orderId}")
    public AjaxResult cancelOrder(@PathVariable("orderId") Long orderId) {

        orderInfoService.cancelOrder(orderId);
        return AjaxResult.success();
    }

    /**
     * 远调接口，根据订单号获取订单信息
     * @param orderNo
     * @return
     */
    @InnerAuth
    @Operation(summary = "根据订单号获取订单信息")
    @GetMapping("/getByOrderNo/{orderNo}")
    public R<OrderInfo> getByOrderNo(@PathVariable("orderNo") String orderNo) {
        OrderInfo orderInfo = orderInfoService.getByOrderNo(orderNo);
        return R.ok(orderInfo);
    }
}
